<template>
    <div @keyup.tab="keyDown" style="-webkit-overflow-scrolling:touch">
        <router-view></router-view>
    </div>
</template>
<style lang="scss">
    @import "./assets/css/main.css";
    // @import "./assets/css/color-dark.css";     /*深色主题*/
    /*@import "./assets/css/theme-green/color-green.css";   浅绿色主题*/
</style>
<script setup lang="ts">
import {loadStyle} from './utils/iconfont';
import { ElMessage } from "element-plus";
import { getCurrentInstance, nextTick, onMounted } from "vue";
import baseService from "./service/baseService";
import { useAppStore } from "@/store/index"
const { proxy } = getCurrentInstance() as any;
const store = useAppStore();

onMounted(()=>{
    handleScroll();
    getIconUrl();
    getPermissionList();
});
let handleScroll= ()=>{
    let scroll = document.querySelectorAll("div[class='content'],.el-table .el-table__body-wrapper");
    if (scroll && scroll.length) {      
        scroll.forEach((item)=>{
            // 监听这个dom的scroll事件
            item.addEventListener('scroll', () => {
                //首先把下拉框下拉的东西清掉
                let nowEle = document.activeElement;                     
                // let select = nowEle.parentNode.parentNode.parentNode;
                let select = nowEle;
                if (select) {                            
                    let pops = document.querySelectorAll(".el-select-dropdown.el-popper,.el-select.el-popper,.el-popover");
                    if (pops.length > 0) {
                        for (let item in pops) {                                  
                            if (pops[item] && pops[item].style && pops[item].style.display != 'none') {
                                pops[item].style.display = 'none';
                                document.click();
                            }
                        }
                    }
                }
            }, false)            
        })    
    }
};
let keyDown=(e)=>{
    let keyCode = e.keyCode;
    let path = e.path;
    let parentsEle = e.target.parentNode.parentNode.parentNode.parentNode.parentNode;
    let inputs = parentsEle.querySelectorAll(".new-number-input");
    let cur = e.target.children[0];
    for(let item in inputs){
        // if(inputs[item] == cur){
            let index = item *1;
            // if(index < inputs.length -1){
                // let nowEle = this.getNextInput(index, inputs);
            let nowEle = cur;
            if(nowEle){
                nowEle.focus();
            }
            // }
        // }
    }
};
let getNextInput=(index, inputs)=>{
    let nowEle = null;

    for (let i = (Number(index) + 1); i < inputs.length; i++) {
        nowEle = inputs[i];
        if (nowEle.disabled) {
            continue;
        }
        let parent = getParentNode(nowEle, 'div.el-select');
        if (parent && parent.item) {
            continue;
        } else {
            break;
        }
    }

    return nowEle;
};
let getParentNode=(node, selector)=>{
    let ret = undefined;
    let row = 0;
    let column = 0;

    let item = node;
    while (item.parentNode.nodeName.toUpperCase() != 'BODY' && !item.parentNode.querySelector(selector)) {                
        let cur = item.parentNode.parentNode.querySelectorAll(selector);
        if (cur && cur.length > 0) {
            let content = Array.prototype.map.call(cur, (e) => {
                return e;
            });
            if (content.includes(item.parentNode)) {
                ret = {rowIndex: row, cellIndex: column, item: item.parentNode}
                break;
            }
        }
        item = item.parentNode
    }

    //1.获取到第几个td
    //2.获取到第几个tr
    return ret
};
//获取iconfont阿里图标
let getIconUrl =()=>{
    baseService.get(`/sys/func/getIconUrl`).then(res=>{
        if(res.code !=0){
            return proxy.$message.error(res.msg);
        }
        let datas = res.data || '';
        if(datas){
            loadStyle(datas);
        }
    })
};
let getPermissionList =()=>{
    let param={
        systemId : 100000
    }
    baseService.get(`/base/sys/function/permissionlist`, param).then(res=>{
        if(res.code !=0){
            return proxy.$message.error(res.msg);
        }
        let datas = res.data || '';
        store.state['permissionList'] = datas;
    });
}
</script>